from main.alarm.models import Alarm
from utils.serializers import CustomModelSerializer
from rest_framework import serializers
from system.project.models import Channel


class AlarmSerializer(CustomModelSerializer):
    """
    微信报警信息 简单序列化器
    """
    channel_name = serializers.CharField(source='channel.channel_name')

    class Meta:
        model = Alarm
        fields = '__all__'
        read_only_fields = ['id']


class AlarmCreateSerializer(CustomModelSerializer):
    """
    微信报警信息记录 简单序列化器
    """
    channel = serializers.CharField()

    def create(self, validated_data):
        channel = Channel.objects.filter(channel_api=validated_data['channel']).first()
        if channel:
            validated_data['channel'] = channel
        return super().create(validated_data)

    class Meta:
        model = Alarm
        fields = '__all__'
        extra_kwargs = {
            'channel': {
                'required': False,
            }
        }
